/*
* Copyright (C) 2017-2018 Alibaba Group Holding Limited. All Rights Reserved.
*
*
* Licensed under the Apache License, Version 2.0 (the License);
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ALIOS_SPEECH_CONST_H_
#define ALIOS_SPEECH_CONST_H_

#include <stdint.h>
#include <string>
#include <memory>

/**
* @file speech_asr.h
* @brief This file contains ASRLocalEngine/ASRCloudEngine public interface defined by AliOS,
* and is shared by the platform independent voice framework and the vendor specific Implementation.
**/

namespace alios {

typedef std::string JSON;
typedef enum {
  Ok = 0,
  // common function error code.
  FuncErrorInput = 2,
  AllocateFailed = 3,
  LocalEngineNotExist = 4,
  InvalidState = 5,

  // file related error code.
  FileNotFound = 10,
  FileOpenFailed = 11,
  FileJsonInvalidFormat = 12,

  // SR related error code.
  SrInitializeFailed = 20,
  SrNotExist = 21,
  SrProcessFailed = 22,
  SrNotInitialized = 23,
  SrStartFailed = 24,
  SrStopFailed = 25,
  SrInvalidState = 26,
  SrRescoreFailed = 27,

  // VAD related error code.
  VadInitializeFailed = 30,
  VadNotExist = 31,
  VadProcessFailed = 32,
  VadNotInitialized = 33,
  VadStartFailed = 34,
  VadStopFailed = 35,

  // AEC related error code.
  AecInitializeFailed = 40,
  AecNotExist = 41,
  AecProcessFailed = 42,
  AecNotInitialized = 43,

  // KWS related error code.
  KwsInitializeFailed = 50,
  KwsNotExist = 51,
  KwsProcessFailed = 52,
  KwsNotInitialized = 53,
  KwsStartFailed = 54,
  KwsStopFailed = 55,

  // Opus related error code.
  EncodeInitializeFailed = 60,
  EncodeNotExist = 61,
  EncodeProcessFailed = 62,
  EncodeNotInitialized = 63,

  // grammar related error code.
  GrammarCreateFailed = 70,
  GrammarInitializeFailed = 71,
  GrammarCompileFailed = 72,
  GrammarNotExist = 73,
  GrammarUnitializeFailed = 74,

  // SV related error code.
  SvCreateFailed = 80,
  SvInitializeFailed = 81,
  SvNotExist = 82,
  SvStartRegisterFailed = 83,
  SvRegisterFailed = 84,
  SvStopRegisterFailed = 85,
  SvStartLoginFailed = 86,
  SvLoginFailed = 87,
  SvStopLoginFailed = 88,
  SvInvalidState = 89,

  // AEC_BEAMFORMER related error code.
  AecBeamformerInitializeFailed = 100,
  AecBeamformerNotExist = 101,
  AecBeamformerProcessFailed = 102,
  AecBeamformerNotInitialized = 103,

  FuncNoSupport = 120,
  UnknownError = 121,
} RetCode;

typedef enum {
  Mandarin = 0,   // using Mandarin
  Cantonese = 1,  // using Cantonese
  SiChuan = 2,    // using SiChuan
  English = 3,    // using English
} LanguageType;

typedef enum {
  AsrJSGF = 0,  // using jsgf
  AsrXBNF = 1,  // using xbnf
} AsrGrammarType;

typedef enum  {
  // sample rate = 8k, sample size = 16bits, channel num = 1.
  Format_8K16BitMono = 0,
  // sample rate = 16k, sample size = 16bits, channel num = 1.
  Format_16K16BitMono = 1,
  // sample rate = 16k, sample size = 16bits, channel num = 2,
  // channel 1 for reference, channel 2 for microphone.
  Format_16K16BitStereo_1Ref_2Mic = 2,
  // channel 1 for microphone, channel 2 for reference.
  Format_16K16BitStereo_1Mic_2Ref = 3,
  // sample rate = 16k, bits_sample = 16, channel_num = 4
  // channel 1 and 2 is for microphone, channel 3 and 4 is for reference.
  Format_16K16BitStereo_12Mic_34Ref = 4,
  Format_16K16BitStereo_4Mic = 5,
  // 1-6 speech channel and 7-8 are the reference channel
  Format_16K16BitStereo_1To6Mic_78Ref = 6,
  Format_16K32BitStereo_1To6Mic_78Ref = 7,
} AudioFormat;

typedef enum {
  Opus = 0,
  OpusOgg = 1,
  Wav = 2,
} AudioEncoder;

typedef enum {
  None = -1, // default none mode.
  Auto = 0,  // using automatic voice activity detection.
  Push2Talk = 1,  // using push to talk, i.e., without vad.
  Kws = 2,  // using key word spotting, then turn to auto mode.
  Kws2Talk = 3,  // using key word spotting, then turn to push2talk mode.
} VadMode ;

typedef enum {
  NoDebug = 0,  // without debug information.
  LogSave = 1,  // save the log information into log.txt.
  WaveSave = 2,  // save the audio data during the processing.
} DebugMode;

typedef enum {
  AsrLogLevelError = 0,
  AsrLogLevelWarning = 1,
  AsrLogLevelInfo = 2,
  AsrLogLevelVerbose = 3,
} AsrLogLevel;

typedef enum {
  AsrWakeupKeywordWav,
  AsrActionKeywordWav,
  AsrPrefixKeywordWav,
  AsrSrWav,
} AsrDebugWavType;

typedef enum {
  AsrNothing = 0,
  AsrPartialResult = 1,
  AsrFinalResult = 2,
  AsrUserCancel = 3,
  AsrError = 4,
} AsrResultState;

typedef enum {
  InvalidKeyword = -1,
  WakeupKeyword = 0,
  ActionKeyword = 1,
  PrefixKeyword = 2,
  NonMainWakeupKeyword = 10,
} KeywordType;

typedef enum {
  KeywordRejected = -1,
  KeywordTrusted = 0,
  KeywordNeedDoubleCheck = 1,
} KeywordCredibility;

typedef enum {
  GenderUnknown = -1,
  GenderFemale = 0,
  GenderMale = 1,
} GenderType;

typedef enum {
  AsrSuccess = 0,
  AsrDefaultError = 1,
  AsrServerNullError = 2,
  AsrConnectionTimeout = 3,
  AsrNetworkInterruptted = 4,
  AsrNetworkClosed = 5,
  AsrLocalServiceNotReady = 6,
  AsrNoLocation = 7,
  AsrSrNetworkErr = 8,
  AsrSrError = 9,
  AsrSrNothing = 10,
  AsrSrNoResult = 11,
  AsrLocalNullResult = 12,
  AsrSrErrOffline = 13,
  AsrUnknown = 14,
  AsrSrErrSslInvalidCertificate = 15,
  AsrCloudUnsupport = 16,
  AsrAskAgain = 17,
  AsrAudioCaptureFailure = 18,
  AsrConfigParseErr = 19,
  AsrAuthErr = 20
} AsrErrorCode;

typedef enum {
  WakeupMode = 0,
  FrontEndMode = 1,
  KwsOnlyMode = 2,
  AsrOnlyMode = 3,
  Push2TalkMode = 4
} StartMode;

typedef enum {
  Invalid = 0,  // not oneshot mode
  WholeAudio = 1,  // oneshot mode and audio data is whole
  CutoffAudio = 2 // oneshot mode and audio data is cut off
} OneShotMode;

typedef enum {
  Append = 0, //origin wakeup word is still active
  Replace = 1 //origin wakeup word is still removed
} KeywordMode;

// left/right/center, front/middle/back
typedef enum {
  InvalidZone = 0,
  FrontLeftZone = 1,
  FrontRightZone = 2,
  MiddleLeftZone = 4,
  MiddleRightZone = 8,
  BackLeftZone = 16,
  BackRightZone = 32,
  FrontZone = FrontLeftZone | FrontRightZone,
  MiddleZone = MiddleLeftZone | MiddleRightZone,
  BackZone = BackLeftZone | BackRightZone,
  LeftZone = FrontLeftZone | MiddleLeftZone | BackLeftZone,
  RightZone = FrontRightZone | MiddleRightZone | BackRightZone,
  WholeZone = LeftZone | RightZone
} VoiceZone;

class KwsResult {
public:
  KeywordType keyword_type;
  KeywordCredibility keyword_credibility;
  std::string key_word;
  int kws_doa;
  VoiceZone kws_vz;
  float confidence;
  GenderType gender_type;
  int channel_id;
  float channel_snr;
  LanguageType lang_type;

  KwsResult(): keyword_type(InvalidKeyword),
    keyword_credibility(KeywordRejected),
    key_word(""),
    kws_doa(0),
    kws_vz(InvalidZone),
    confidence(0.f),
    gender_type(GenderUnknown),
    lang_type(Mandarin),
    channel_id(-1),
    channel_snr(0.f)
  {}
};

}

#endif //ALIOS_SPEECH_CONST_H_
